package com.github.pig.admin.controller.zhgl.notification;

import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.model.zhgl.notification.NotificationInfo;
import com.github.pig.admin.service.sys.SysUserService;
import com.github.pig.admin.service.zhgl.notification.NotificationInfoService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.R;
import com.github.pig.common.vo.UserVO;
import com.github.pig.common.web.BaseController;
import io.jsonwebtoken.Claims;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 通知公告 前端控制器
 * </p>
 *
 * @author xiaosa
 * @since 2018-07-10
 */
@RestController
@RequestMapping("/notificationInfo")
public class NotificationInfoController extends BaseController {
    @Autowired
    private NotificationInfoService notificationInfoService;
    @Autowired
    private SysUserService sysUserService;
    /*@Autowired
    private SysCommunityUserService sysCommunityUserService;

    @Autowired
    private SysCommunityUserController sysCommunityUserController;
    @Autowired
    private ICommunityInfoService communityInfoService;*/
    /**
    * 通过ID查询
    *
    * @param id ID
    * @return NotificationInfo
    */
    @PostMapping("/getObj/{id}")
    public R<NotificationInfo> get(@PathVariable String id) {
        NotificationInfo info = notificationInfoService.selectById(id);
        Document doc = Jsoup.parse(info.getBody());
        Elements imageList = doc.select("img"); //处理的切入点，找img标签
        if (imageList != null && imageList.size() > 0) { //图片
            for (Element e : imageList) {
                String image = e.toString();
                image = image.replace("<img src=\"","");
                image = image.replace("\">", "");
                e.attr("src",CommonConstant.FILE_ADDRESS_GET+image);
            }
            info.setBody(doc.toString());
        }

        return new R<>(info);
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params,NotificationInfo notificationInfo) {
        Page<NotificationInfo> page = new Page<NotificationInfo>(Integer.parseInt(params.get("page").toString()),
                Integer.parseInt(params.get("limit").toString()));
        //获取当前登陆人ID
        // 获取当前登录的用户
        Claims claims = getCurrentUserInfo();
        String currentUserName = (String) claims.get("user_name");
        UserVO userVO = sysUserService.findUserByUsername(currentUserName);
        if(!CommonConstant.MANAGE_NAME.equals(currentUserName)){
            notificationInfo.setCreateBy(userVO.getUserId()+"");
        }
        return notificationInfoService.selectPages(page,notificationInfo);
    }

    /**
     * 添加
     * @param  notificationInfo  实体
     * @return success/false
     */
    @PostMapping
    public AjaxJson add(@RequestBody NotificationInfo notificationInfo) {
        notificationInfo.setCreateBy(this.getUserName());
        notificationInfo.setCreateDate(new Date());
        return notificationInfoService.add(notificationInfo);
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable String id) {
        NotificationInfo notificationInfo = new NotificationInfo();
        notificationInfo.setId(id);
        notificationInfo.setUpdateDate(new Date());
        notificationInfo.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(notificationInfoService.updateById(notificationInfo));
    }

    /**
     * 编辑
     * @param  notificationInfo  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody NotificationInfo notificationInfo) {
        notificationInfo.setUpdateDate(new Date());
        return new R<>(notificationInfoService.update(notificationInfo));
    }

}
